import util from '../../../utils/util.js';
import $ from '../../../utils/ajax.js';
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        sictureSet: {
            businessLicense: 'https://xfjwwwimg.oss-cn-shanghai.aliyuncs.com/WX/83.png',
            permitCertificates: 'https://xfjwwwimg.oss-cn-shanghai.aliyuncs.com/WX/80.png',
            idCardPositive: 'https://xfjwwwimg.oss-cn-shanghai.aliyuncs.com/WX/82.png',
            idCardOpposite: 'https://xfjwwwimg.oss-cn-shanghai.aliyuncs.com/WX/81.png',
        },
        onDate: {
            businessLicense: '',
            permitCertificates: '',
            idCardPositive: '',
            idCardOpposite: '',
        },
        item: {
            sh: false,
            name: '',
            image: '',
            onSu: () => { },
        },
        imageUrl: '',
        items: {},
        second: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let second = options.second ? Number(options.second) : 0;
        if (second === 100) {
            this.setData({
                items: JSON.parse(options.item),
                second: second
            })
        } else {
            let prime = JSON.parse(options.item);
            const onDate = {
                businessLicense: prime.businessLicense || '',
                permitCertificates: prime.permitCertificates || '',
                idCardPositive: prime.idCardPositive || '',
                idCardOpposite: prime.idCardOpposite || '',
            }

            $.p(app.wxApp.url + 'agentnew/imgPre', {}, () => { }, (res) => {
                const sictureSet = {
                    businessLicense: prime.businessLicense ? res.data + prime.businessLicense : 'https://xfjwwwimg.oss-cn-shanghai.aliyuncs.com/WX/83.png',
                    permitCertificates: prime.permitCertificates ? res.data + prime.permitCertificates : 'https://xfjwwwimg.oss-cn-shanghai.aliyuncs.com/WX/80.png',
                    idCardPositive: prime.idCardPositive ? res.data + prime.idCardPositive : 'https://xfjwwwimg.oss-cn-shanghai.aliyuncs.com/WX/82.png',
                    idCardOpposite: prime.idCardOpposite ? res.data + prime.idCardOpposite : 'https://xfjwwwimg.oss-cn-shanghai.aliyuncs.com/WX/81.png',
                }
                this.setData({
                    onDate: onDate,
                    items: prime,
                    sictureSet: sictureSet,
                    imageUrl: res.data,
                })
            })
        }
        
        
    },
    onTip(e) {
        const _this = this;
        const id = e.currentTarget.id;
        const choose = {
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['camera', 'album'],
        };
        let names = '';
        let images = '';
        let onTipDate = {};
        switch (id) {
            case 'idCardPositive':
                names = '法人身份证正面照';
                images = 'https://xfj.oss-cn-shanghai.aliyuncs.com/agent/guide/shenfenzhengzhengmian.png';
                onTipDate = (res) => {
                    return {
                        ['sictureSet.idCardPositive']: res.data,
                        ['onDate.idCardPositive']: res.imagePath
                    }
                }
                break;
            case 'idCardOpposite': 
                names = '法人身份证反面照';
                images = 'https://xfj.oss-cn-shanghai.aliyuncs.com/agent/guide/shenfenzhengbeimian.png';
                onTipDate = (res) => {
                    return {
                        ['sictureSet.idCardOpposite']: res.data,
                        ['onDate.idCardOpposite']: res.imagePath
                    }
                }
                break;

            case 'businessLicense':
                names = '营业执照';
                images = 'https://xfj.oss-cn-shanghai.aliyuncs.com/agent/guide/sanzhengheyi.png';
                onTipDate = (res) => {
                    return {
                        ['sictureSet.businessLicense']: res.data,
                        ['onDate.businessLicense']: res.imagePath
                    }
                }
                  
                break;

            case 'permitCertificates':
                names = '开户行许可证';
                images = 'https://xfj.oss-cn-shanghai.aliyuncs.com/agent/guide/kaihuxukezheng.png';
                onTipDate = (res) => {
                    return {
                        ['sictureSet.permitCertificates']: res.data,
                        ['onDate.permitCertificates']: res.imagePath
                    }
                }
                break;
        }
        wx.chooseImage({
            ...choose,
            success(res) {
                $.u(app.wxApp.url + 'upload/uploadImage2', res.tempFilePaths[0], {
                    'xfjtk': wx.getStorageSync('xfjtk')
                }, app.onStrart, (res) => {
                    _this.setData({
                        ...onTipDate(res),
                    });
                    util.showToast('上传成功')
                }, (res) => {
                    util.showToast(res)
                })
            }
        })
    },
    onPrev() {
        $.p(app.wxApp.url + 'agentnew/backStep', {
            tel: wx.getStorageSync('tel'),
            agent: wx.getStorageSync('agent'),
            type: 2,
        }, () => { }, (res) => {
            app.agentStatus();
        })
    },
    onSubmit() {
        const that = this;
        const prime = this.data.onDate;
        const multiArray = this.data.multiArray;
        const multiIndex = this.data.multiIndex;
        const agentType = this.data.agentType;
        const second = this.data.second;
        const items = this.data.items;
        const generalCheck = util.nonEmpty(prime.businessLicense, '请上传营业执照') &&
            util.nonEmpty(prime.idCardPositive, '请上传法人身份证正面') &&
            util.nonEmpty(prime.idCardOpposite, '请上传法人身份证反面');

        if (generalCheck) {
            if (items.role === 3 ) {
                this.onSave(prime, multiArray, multiIndex, second, items);
            } else {
                wx.showModal({
                    title: '温馨提示',
                    content: '亲，记得支付运营费用哦',
                    cancelText: '取消',
                    confirmText: '确认',
                    confirmColor: '#42bcfd',
                    success(res) {
                        if (res.confirm) {
                            that.onSave(prime, multiArray, multiIndex, second, items);
                        } else if (res.cancel) {
                            console.log('用户点击取消')
                        }
                    }
                })
            }
        }  
    },
    onSave(prime, multiArray, multiIndex, second, items){
        if (second === 100) {
            const role = {
                ...items,
                ...prime,
                agent: wx.getStorageSync('agent'),
            }
            $.p(app.wxApp.url + 'agentnew/twoSaveRole', {
                role: JSON.stringify(role),
            }, app.onStrart, (res) => {
                wx.navigateBack({
                    delta: 3
                })
                wx.showToast({
                    title: '运营商申请提交成功',
                    icon: 'none',
                    duration: 3000
                });
            }, (res) => {
                util.showToast(res.data)
            })
        } else {
            const role = {
                businessLicense: prime.businessLicense,
                permitCertificates: prime.permitCertificates,
                idCardPositive: prime.idCardPositive,
                idCardOpposite: prime.idCardOpposite,
                agent: wx.getStorageSync('agent'),
                role: items.role
            }
            $.p(app.wxApp.url + 'agentnew/saveRole', {
                role: JSON.stringify(role),
                tel: wx.getStorageSync('tel'),
            }, app.onStrart, (res) => {
                app.agentStatus();
            }, (res) => {
                util.showToast(res.data)
            })
        }
    }
})